package com.omicrondev.flightops;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.ListIterator;

/**
 * Created by gdean on 7/29/13.
 */
public class AcType {
    public ArrayList<CGLimit> mFwdCG;
    public ArrayList<CGLimit> mAftCG;
    public ArrayList<CGStation> mStations;

    public int mBEW = 0;
    public double mBEA = 0;

    public int mZfLimit = 0;
    public int mToLimit = 0;
    public int mLdgLimit = 0;

    public AcType() {
    }

    public Boolean buildFromJSON(String json)
    {
        try {
            JSONObject jsonObject = new JSONObject(json);

            mBEW = jsonObject.getInt("bew");
            mBEA = jsonObject.getDouble("bea");
            mToLimit = jsonObject.getInt("toLimit");
            mLdgLimit = jsonObject.getInt("ldgLimit");
            mZfLimit = jsonObject.getInt("zfLimit");

            JSONArray jsonLimits = jsonObject.getJSONArray("fwdLimits");

            JSONObject jsonLimit;
            mFwdCG = new ArrayList<CGLimit>();
            for (int index = 0; index < jsonLimits.length(); index++) {
                jsonLimit = jsonLimits.getJSONObject(index);

                mFwdCG.add(new CGLimit(jsonLimit.getInt("weight"), jsonLimit.getDouble("arm")));
            }

            jsonLimits = jsonObject.getJSONArray("aftLimits");

            mAftCG = new ArrayList<CGLimit>();
            for (int index = 0; index < jsonLimits.length(); index++) {
                jsonLimit = jsonLimits.getJSONObject(index);

                mAftCG.add(new CGLimit(jsonLimit.getInt("weight"), jsonLimit.getDouble("arm")));
            }

            JSONArray jsonStations = jsonObject.getJSONArray("stations");

            JSONObject jsonStation;
            mStations = new ArrayList<CGStation>();
            for (int index = 0; index < jsonStations.length(); index++) {
                jsonStation = jsonStations.getJSONObject(index);

                mStations.add(new CGStation(jsonStation.getString("name"),
                        jsonStation.getDouble("arm"),
                        jsonStation.getInt("min"),
                        jsonStation.getInt("max"),
                        jsonStation.getBoolean("fuel"),
                        jsonStation.getBoolean("hidden")));
            }
        } catch (JSONException e) {
            return Boolean.FALSE;
        }

        return true;
    }

    public String saveToJSON()
    {
        String json = new String();

        try {
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("bew", mBEW);
            jsonObject.put("bea", mBEA);
            jsonObject.put("toLimit", mToLimit);
            jsonObject.put("ldgLimit", mLdgLimit);
            jsonObject.put("zfLimit", mZfLimit);

            // Forward CG Limits

            JSONArray jsonLimits = new JSONArray();

            ListIterator<CGLimit> itr = mFwdCG.listIterator();
            CGLimit limit;

            while (itr.hasNext()) {
                limit = itr.next();

                JSONObject jsonLimit = new JSONObject();

                jsonLimit.put("weight", limit.mWeight);
                jsonLimit.put("arm", limit.mArm);

                jsonLimits.put(jsonLimit);
            }

            jsonObject.put("fwdLimits", jsonLimits);

            // Aft CG Limits

            jsonLimits = new JSONArray();

            itr = mAftCG.listIterator();

            while (itr.hasNext()) {
                limit = itr.next();

                JSONObject jsonLimit = new JSONObject();

                jsonLimit.put("weight", limit.mWeight);
                jsonLimit.put("arm", limit.mArm);

                jsonLimits.put(jsonLimit);
            }

            jsonObject.put("aftLimits", jsonLimits);

            // CG Stations

            JSONArray jsonStations = new JSONArray();

            ListIterator<CGStation> stationItr = mStations.listIterator();
            CGStation station;
            while (stationItr.hasNext()) {
                station = stationItr.next();

                JSONObject jsonStation = new JSONObject();

                jsonStation.put("name", station.mName);
                jsonStation.put("arm", station.mArm);
                jsonStation.put("min", station.mMinWeight);
                jsonStation.put("max", station.mMaxWeight);
                jsonStation.put("fuel", station.mIsFuel);
                jsonStation.put("hidden", station.mIsHidden);

                jsonStations.put(jsonStation);
            }

            jsonObject.put("stations", jsonStations);

            json = jsonObject.toString();
        } catch (JSONException e) {
            //return "";
        }

        return json;
    }

    public double getFwdLimit(int weight)
    {
        return getLimit(weight, mFwdCG);
    }

    public double getAftLimit(int weight)
    {
        return getLimit(weight, mAftCG);
    }

    private double getLimit(int weight, ArrayList<CGLimit> limits)
    {
        ListIterator<CGLimit> itr = limits.listIterator();
        CGLimit start = itr.next();
        CGLimit end = null;
        while (itr.hasNext()) {
            end = itr.next();
            if (weight < end.mWeight) {
                // Interpolate CG Limit at given weight
                return (start.mArm + (end.mArm - start.mArm) * (weight - start.mWeight) / (end.mWeight - start.mWeight));
            }

            start = end;
        }

        return 0;
    }

}
